package com.qingneng.web;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.mvel2.MVEL;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;
import com.qingneng.service.sdk.IWorkBudgetService;
import com.qingneng.vo.BudgetQuantitiesBuildItem;
import com.qingneng.vo.BudgetWorkQuantities;
import com.qingneng.vo.QantitiesBuildItem;
import com.tbfs.common.BaseAction;
import com.tbfs.util.UtilTool;

public class WorkQuantitiesAction extends BaseAction  implements ModelDriven<BudgetWorkQuantities>{
	
	private static final Logger logger = Logger.getLogger(WorkQuantitiesAction.class);
	
	public BudgetWorkQuantities budgetWorkQuantities = new BudgetWorkQuantities();
	
	public IWorkBudgetService workBudgetService;
	
	public WorkQuantitiesAction(){
		
	}
	public WorkQuantitiesAction(IWorkBudgetService workBudgetService){
		this.workBudgetService = workBudgetService;
	}
	
	public BudgetWorkQuantities getModel() {
		return budgetWorkQuantities;
	}
	
	public String calQuantities(){
			if(logger.isDebugEnabled()){
				logger.debug("+++begin：cal work +++");
			}
			
			HttpServletRequest request = getRequest();
			String quantitiesBuildItemList = request.getParameter("quantitiesBuildItemList");		
			logger.info("quantitiesBuildItemList--------="+quantitiesBuildItemList);
			Gson json = new Gson();
			//转换对象
			List<QantitiesBuildItem> list = json.fromJson(quantitiesBuildItemList, new TypeToken<List<QantitiesBuildItem>>() {
			}.getType());
			Double ws_capa = 0d;
			Double wn_capa = 0d;
			Double yx_capa = 0d;
			if(list!=null && list.size()>0){
				for(Iterator<QantitiesBuildItem> iter = list.iterator();iter.hasNext();){
					QantitiesBuildItem item = iter.next();
					String v_data  =  item.getV_data();	
					if(v_data!=null && !"".equals(v_data)){
						
					String a_value = item.getA_value();
					String b_value = item.getB_value();
					String c_value = item.getC_value();
					String K_value = item.getK_value();
					String TT_value = item.getTT_value();
					String t_value = item.getT_value();
					String q_value = item.getQ_value();
					String α_value = item.getΑ_value();
					
					String item_formula_ws = item.getItem_formula_ws();
					String item_formula_wn = item.getItem_formula_wn();
					//污水容积
					Map<String, Object> ws_vars = new HashMap<String, Object>();   
					ws_vars.put("N2", new Double(v_data));
					ws_vars.put("α", new Double(α_value));
					ws_vars.put("q", new Double(q_value));
					ws_vars.put("t", new Double(t_value));
			        //相加
			        ws_capa = ws_capa+(Double )MVEL.eval(item_formula_ws, ws_vars); 
			        //污泥容积
			        
			        Map<String, Object> wn_vars = new HashMap<String, Object>();   
			        wn_vars.put("N2", new Double(v_data));
			        wn_vars.put("α", new Double(α_value));
			        wn_vars.put("a", new Double(a_value));
			        wn_vars.put("b", new Double(b_value));
			        wn_vars.put("c", new Double(c_value));
			        wn_vars.put("T", new Double(TT_value));
			        wn_vars.put("K", new Double(K_value));
					
			      //相加
			        wn_capa = wn_capa + (Double )MVEL.eval(item_formula_wn, wn_vars); 
					}
			             
				}
			}
			//有效容积
			yx_capa = ws_capa+wn_capa;
			
			budgetWorkQuantities.setWasteWaterCapacity(UtilTool.decimalFormat(ws_capa));
			budgetWorkQuantities.setSludgeCapacity(UtilTool.decimalFormat(wn_capa));
			budgetWorkQuantities.setEffectiveCapacity(UtilTool.decimalFormat(yx_capa));
			 if(logger.isDebugEnabled()){
					logger.debug("+++end：cal work +++");
				}
			return Action.SUCCESS;	
		}
		
	public String saveQuantities(){
			if(logger.isDebugEnabled()){
				logger.debug("+++ begin execute savaQuantities  +++");
			}
			//删除当前工程量结果信息
			logger.info("+++ delete old  budgetWorkQuantities +++ ");
			workBudgetService.destroyWorkQuantities(budgetWorkQuantities);
			
			//工程量建筑类型
			HttpServletRequest request = getRequest();
			String quantitiesBuildItemList = request.getParameter("quantitiesBuildItemList");
			//转换对象
			Gson json = new Gson();
			List<BudgetQuantitiesBuildItem> buildItemlist = json.fromJson(quantitiesBuildItemList, new TypeToken<List<BudgetQuantitiesBuildItem>>() {
			}.getType());
		
			logger.info("+++ insert new  budgetWorkQuantities +++ ");
			try {
				workBudgetService.addWorkQuantities(budgetWorkQuantities);
				//保存工程量建筑类型
				if(buildItemlist!=null && buildItemlist.size()>0){
					for(Iterator<BudgetQuantitiesBuildItem> iter = buildItemlist.iterator();iter.hasNext();){
						BudgetQuantitiesBuildItem bean = iter.next();
						bean.setProject_workinfo_id(budgetWorkQuantities.getProject_workinfo_id());
						workBudgetService.addBudgetQuantitiesBuildItem(bean);
					}
				}
				
				//保存成功
				budgetWorkQuantities.setMessage("true");
			} catch (Exception e) {
				budgetWorkQuantities.setMessage("false");
				e.printStackTrace();
			}
			
			
			if(logger.isDebugEnabled()){
				logger.debug("+++ end execute savaQuantities  +++");
			}
			return Action.SUCCESS;
		}
	
	
	public String calAndsaveAvg(){
		if(logger.isDebugEnabled()){
			logger.debug("+++ begin execute calAndsaveAvg  +++");
		}
		workBudgetService.updateWorkQuantities(budgetWorkQuantities);
		
		if(logger.isDebugEnabled()){
			logger.debug("+++ end execute calAndsaveAvg  +++");
		}
		return Action.SUCCESS;
	}
	
	
	
		
		public String loadQuantities(){
			if(logger.isDebugEnabled()){
				logger.debug("+++ begin execute loadQuantities  +++");
			}
			List list = workBudgetService.getWorkQuantities(budgetWorkQuantities);
			if(list!=null && list.size()>0){
				BudgetWorkQuantities bean = (BudgetWorkQuantities)list.get(0);
				budgetWorkQuantities.setEffectiveCapacity(bean.getEffectiveCapacity());
				budgetWorkQuantities.setSludgeCapacity(bean.getSludgeCapacity());
				budgetWorkQuantities.setWasteWaterCapacity(bean.getWasteWaterCapacity());
				budgetWorkQuantities.setNum(bean.getNum());
				budgetWorkQuantities.setAvgCapacity(bean.getAvgCapacity());
			}
			if(logger.isDebugEnabled()){
				logger.debug("+++ end execute loadQuantities  +++");
			}
			return Action.SUCCESS;
		}
}
